/*
 * This file contains all the javascript functions that use by
 * Syn project management plugin in Synergy digital asset management
 * system.
 * Create by: Weranga Kaluarachchi
 * Date: 18/04/2010
 */



/* This method is using to toggle visibility of any div element.
 * @param	ID of the div element wants to change visibility
 * @param	object that trigger the function
 * @param	string that display when div is collapse
 * @param	string that display when div is expand
 */
function toggleProject(objID, me, hide, show) { 
  if (document.getElementById(objID).style.display == "none") { 
  	document.getElementById(objID).style.display="block"; 
  	me.innerHTML = hide;
  } 
  else {
	document.getElementById(objID).style.display="none"; 
    me.innerHTML = show;
  }
}


/* This method submits the form with selected project ID.
 * @param	ID of the selected project
 */
function selectProj(id) {
	var inputId = document.createElement("INPUT");
	inputId.type = "hidden";
	inputId.value = id;
	inputId.name = "syn_project_select_id";
	inputId.id = "syn_project_select_id";
	document.getElementById("hiddenForm").appendChild(inputId);
	// submit the form
	document.getElementById("hiddenForm").submit();
}


/* Checks new category already exists.
 * @param	element	wants to check
 * @param	element check against
 * @return	true or false
 */
function checkNewKeyword(textBox, listBox) {
	var value = document.getElementById(textBox).value;
	var lb = document.getElementById(listBox);
	//for(var x in lb.value) {
	for(var i=0; i<lb.length; i++) {
		if(value.toUpperCase() == lb.options[i].text.toUpperCase()) {
			alert("The category already in the list!");
			return false;
		}
	}
	return true;
}


/*
 * Populate information of the selected project to edit.
 * @param	int	project ID
 */
function editProjInfo(projID) {	
	// return if element does not exist
	if(document.getElementsByName('btnSynProjModify') == null) {
        return;
    }
    var lb = document.getElementById('ldbSynProjEditKeyword'+projID);
	var projName = document.getElementById('tmpProjName'+projID).value;
    var projDescription = document.getElementById('tmpProjInfo'+projID).value;
    var keyWords = document.getElementById('tmpProjKeyword'+projID).value;
    var keyWord = keyWords.split("#");
   
    // deselect all the categories in the list box
    for(var i = 0; i < lb.length; i++){
    	lb.options[i].selected = false;
    }
    // reselect categories belongs to the selecetd project
    for(var i = 0; i < lb.length; i++){
	    for(var x in keyWord) {
	    	if(lb.options[i].value==keyWord[x]) {
	   	 		lb.options[i].selected = true;
	   	 		break;
	   	 	}
	    }
    }
    
    document.getElementById('txtSynProjName'+projID).value = projName;
    // set project ID into a hidden variable
    document.getElementById('hdProjID'+projID).value = projID;
    document.getElementById('txaSynProjInfo'+projID).value = projDescription;  
    
    //set visibility of form and edit project div
    if(document.getElementById('dvSynProjEditProject'+projID).style.display == "none") {
		document.getElementById('dvSynProjEditProject'+projID).style.display="block"; 
	}
	else if(document.getElementById('dvSynProjEditProject'+projID).style.display == "block") {
		document.getElementById('dvSynProjEditProject'+projID).style.display="none"; 
	}   
  
}


/*
 * Add/Remove contributors to/from the selected project
 * @param	int	project ID
 */
function updateContributorsOfProject(projID) {
	// return if element does not exist
	if(document.getElementsByName('btnSynProjAddContributor') == null) {
        return;
    }  	

	var projContributors = document.getElementById('tmpProjContributors'+projID).value;
    var projContributor = projContributors.split("#");
   
    chk = document.getElementsByName('chkAllUsers[]');
    // uncheck all the contributors in the list box
    for (i = 0; i < chk.length; i++) {
    	chk[i].checked = false ;
    }
    // reselect contributors belongs to the selecetd project
    for(var i = 0; i < chk.length; i++){    	
	    for(var x in projContributor) {
	    	if(chk[i].value == projContributor[x]) {
	    		chk[i].checked = true;
	   	 		break;
	   	 	}
	    }
    }
    // set project ID into a hidden variable
	document.getElementById('hdProjID'+projID).value = projID;
	
	//set visibility of form and add contributors div
	if(document.getElementById('dvSynProjAddContributors'+projID).style.display == "none") {
		document.getElementById('dvSynProjAddContributors'+projID).style.display="block"; 
	}
	else if(document.getElementById('dvSynProjAddContributors'+projID).style.display == "block") {
		document.getElementById('dvSynProjAddContributors'+projID).style.display="none"; 
	}  
	
	
  
}


/*
 * Confirm delete of the selected project
 * @return true or false
 */
function confirmProjectDelete() {
	// return if element does not exist
	if(document.getElementsByName('btnSynProjDelete') == null) {
        return;
    }
    var isDelete = false;
    if(confirm('Do you really want to delete this project?')) {
    	isDelete = true;    
    }
    return isDelete;
}


/*
 * Validate the project name and the project information before send to the server
 * @return true or false 
 */
function validateProject() {	
	if (trimProject(document.getElementById('txtSynProjectName').value)  == "") {
		alert('Project name is blank!');
		return false;
	}
	else if(trimProject(document.getElementById('txaSynProjectInfo').value) == "") {
		alert('Project information is blank!');
		return false;
	}
	else if(!checkNewKeyword('txtSynNewCategory', 'ldbSynProjKeywords')) {
		return false;
	}

}


/*
 * Validate the project name and the project information before update
 * @param	int	project ID
 * @return 	true or false 
 */
function validateEditProject(projID) {	
	if (trimProject(document.getElementById('txtSynProjName'+projID).value)  == "") {
		alert('Project name is blank!');
		return false;
	}
	else if(trimProject(document.getElementById('txaSynProjInfo'+projID).value) == "") {
		alert('Project information is blank!');
		return false;
	}
	else if(!checkNewKeyword('txtSynProjAddEditKeyword'+projID, 'ldbSynProjEditKeyword'+projID)) {
		return false;
	}
}


/*
 * Validate the maximum length of a component
 * @param	componenet	component that wants to validate
 * @param	int	maximum length 
 */
function textProjInfoLimit(field, maxlen) {
	if (field.value.length > maxlen) {
		alert('Project information has exceeded the maximum length!');
		return;
	}

}


/*
 * Trim a string at the begining and at the end
 * @param	string which wants to trim
 * @return	string after trim 
 */
function trimProject(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}

